package com.zy.pro.order.web;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.city.model.City;
import com.zy.pro.city.service.CityService;
import com.zy.pro.city.web.form.CitySearchForm;
import com.zy.pro.order.model.OrderItem;
import com.zy.pro.order.service.OrderItemService;
import com.zy.pro.order.web.form.OrderItemSearchForm;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.model.ShopUser;
/**
 * 订单管理控制器
 *
 */
@Controller
@RequestMapping("/control/order/orderItemController")
public class OrderItemController extends BaseController {

	private static final Logger logger = Logger.getLogger(OrderItemController.class);
	
	/**
	 * 订单管理服务类
	 */
	@Resource(name = "orderItemServiceImpl")
	private OrderItemService orderItemService;
	
	/**
	 * 城市管理服务类
	 */
	@Resource(name = "cityServiceImpl")
	private CityService cityService;
	
	/**
	 * 系统-列出订单列表管理
	 */
	@RequestMapping("/sysOrderList")
	public String sysOrderList(OrderItemSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			
			if(form.getOrderShopName() != null && form.getOrderShopName() != ""){
				form.setOrderShopName(form.getOrderShopName().trim());
			}
			
			if(form.getOrderId() != null && form.getOrderId() != ""){
				form.setOrderId(form.getOrderId().trim());
			}
			
			form.getOrderby().put("orderCreateTime", "desc");
			
			//创建页面对象
			PageView<OrderItem> pageView = new PageView<OrderItem>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(orderItemService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/order/orderItemController/sysOrderList.do");
			
			CitySearchForm cForm = new CitySearchForm();
			cForm.getOrderby().put("cityCreateTime", "desc");
			List<City> cityList = cityService.findAll(cForm);
			map.addAttribute("cityList", cityList);
			
			return "/control/system/order/sysOrderList";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-显示订单详情管理
	 */
	@RequestMapping("/dispalySysDetail")
	public String dispalySysDetail(OrderItem order, ModelMap map) {
		try {
			
			/*CitySearchForm cForm = new CitySearchForm();
			cForm.getOrderby().put("cityCreateTime", "desc");
			List<City> cityList = cityService.findAll(cForm);*/
			
			order = orderItemService.findOrderDetail(order.getOrderId());
			map.addAttribute("order", order);
			
			//map.addAttribute("cityList", cityList);
			return "/control/system/order/sysDetailOrder";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-删除单个订单管理
	 */
	@RequestMapping("/deletedOrder")
	public void deletedOrder(OrderItemSearchForm form, ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			OrderItem o = orderItemService.find(form.getOrderId());
			if(o != null){
				OrderItem order = new OrderItem();
				order.setOrderId(form.getOrderId());
				order.setOrderStatus(3);//1:正常,3:删除状态
				orderItemService.updateBySelective(order);
//				orderItemService.delete(form.getOrderId());
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
}
